package com.qyh.lucas;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qyh.lucas.entity.StatusEntity;
import com.qyh.lucas.fsm.FsmHandler;
import com.qyh.lucas.model.Fsm;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

@Slf4j
public class TestMain {

    private static final String FSM_JSON = "{\"statusFieldNames\":[\"monitorStatus\",\"canLetter\",\"pauseLetterStatus\",\"worksItemStatus\",\"workCertStatus\"],\"statusAliasMapping\":{\"monitorStatus\":{\"未监测\":0,\"监测中\":1,\"暂停监测\":2,\"发函中\":3,\"发函失败\":4,\"暂停发函\":5,\"监测失败\":6},\"canLetter\":{\"未发函\":0,\"已发函\":1},\"pauseLetterStatus\":{\"未暂停\":0,\"已暂停\":1},\"worksItemStatus\":{\"未开始\":0,\"进行中\":1,\"已完成\":2},\"workCertStatus\":{\"未申请\":0,\"已申请\":1,\"已颁发\":2}},\"definitions\":[{\"from\":{\"monitorStatus\":\"未监测\",\"canLetter\":\"未发函\",\"pauseLetterStatus\":\"未暂停\",\"worksItemStatus\":\"未开始\",\"workCertStatus\":\"未申请\"},\"transitions\":[{\"event\":\"发起监测不发函\",\"to\":{\"monitorStatus\":\"监测中\",\"canLetter\":\"未发函\",\"pauseLetterStatus\":\"未暂停\",\"worksItemStatus\":\"未开始\",\"workCertStatus\":\"未申请\"}},{\"event\":\"发起监测并发函\",\"to\":{\"monitorStatus\":\"监测中\",\"canLetter\":\"已发函\",\"pauseLetterStatus\":\"已暂停\",\"worksItemStatus\":\"未开始\",\"workCertStatus\":\"未申请\"}}]}]}";

    public static void main(String[] args) throws JsonProcessingException {
        // 初始化状态机
        ObjectMapper objectMapper = new ObjectMapper();
        Fsm fsm = objectMapper.readValue(FSM_JSON, Fsm.class);

        // 初始状态
        StatusEntity statusEntity = StatusEntity.builder()
                .pauseLetterStatus(0)
                .canLetter(0)
                .monitorStatus(0)
                .worksItemStatus(0)
                .workCertStatus(0)
                .build();

        // 调用 rpc
        // 如果返回值为 1  发起监测并发函
        // 如果返回值为 0  发起监测不发函
        int i = mockRpc();
        if (i == 1) {
            FsmHandler.trans(fsm, statusEntity, TransferEventEnum.MONITORING_AND_SENDING_LETTER);
        } else {
            FsmHandler.trans(fsm, statusEntity, TransferEventEnum.MONITORING_WITHOUT_SENDING_LETTER);
        }
    }

    public static void testNoFSM(String[] args) throws JsonProcessingException {

        // 初始状态
        StatusEntity statusEntity = StatusEntity.builder()
                .pauseLetterStatus(0)
                .canLetter(0)
                .monitorStatus(0)
                .worksItemStatus(0)
                .workCertStatus(0)
                .build();

        // 调用 rpc
        // 如果返回值为 1  发起监测并发函
        // 如果返回值为 0  发起监测不发函
        int i = mockRpc();
        if (i == 1) {
            statusEntity = StatusEntity.builder()
                    .pauseLetterStatus(0)
                    .canLetter(0)
                    .monitorStatus(0)
                    .worksItemStatus(0)
                    .workCertStatus(1)
                    .build();
        } else {
            statusEntity = StatusEntity.builder()
                    .pauseLetterStatus(0)
                    .canLetter(1)
                    .monitorStatus(1)
                    .worksItemStatus(0)
                    .workCertStatus(1)
                    .build();
        }
    }


    // mock for rpc
    private static int mockRpc() {
        return new Random().nextInt(2);
    }

}
